package com.qqt.csr.order.service.statemachine.condition.impl;

import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.utils.ServiceAssert;
import com.qqt.csr.order.entity.WorkOrder;
import com.qqt.csr.order.enums.WorkOrderEventEnum;
import com.qqt.csr.order.enums.WorkOrderStatusEnum;
import com.qqt.csr.order.service.statemachine.condition.ISubCondition;
import com.qqt.csr.order.service.statemachine.context.WorkOrderContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class CreatedWorkOrderSubCondition implements ISubCondition {

    @Override
    public boolean matches(WorkOrderEventEnum event) {
        return WorkOrderEventEnum.CREATED.equals(event) || WorkOrderEventEnum.CREATED_ALLOCATED.equals(event);
    }

    @Override
    public boolean isSatisfied(WorkOrderContext context) {
        WorkOrder order = context.getToOrder();
        ServiceAssert.notNull(order, StatusCode.Common.NOT_EXIST.getCode(), "创建工单不能为空！");
        if (!WorkOrderStatusEnum.NONE.getStatus().equals(order.getStatus())) {
            return false;
        }
        ServiceAssert.isTrue(!order.isTimeOutConfig() || (order.getTimeoutConfig() != null && order.getTimeoutConfig() > 0), StatusCode.Common.ILLEGAL.getCode(), "工单超时时间不能为空！");
        ServiceAssert.notNull(order.getOrigin(), StatusCode.Common.ILLEGAL.getCode(), "工单来源不能为空！");
        ServiceAssert.isTrue(!order.isBindConversation() || StringUtils.isNotBlank(order.getConversationId()), StatusCode.Common.ILLEGAL.getCode(), "关联的会话id不能为空！");

        return true;
    }
}
